package com.filex.ads.nativeads;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;

public class NativeAd {

    public interface NativeAdEventListener {
        void onAdImpression();
        void onAdClick();
    }

    private boolean mRecordedImpression;
    private boolean mIsDestroyed;

    final private String mPlacementId;
    final private BaseNativeAd mBaseNativeAd;

    @Nullable
    private NativeAdEventListener mNativeAdEventListener;

    public NativeAd(@NonNull String placementId,
                    @NonNull BaseNativeAd baseNativeAd) {

        this.mBaseNativeAd = baseNativeAd;
        this.mPlacementId = placementId;
        baseNativeAd.setNativeEventListener(new BaseNativeAd.NativeEventListener() {
            @Override
            public void onAdImpressed() {
                notifyImpression();
            }

            @Override
            public void onAdClicked() {
                notifyClick();
            }
        });
    }

    public void setNativeAdEventListener(@Nullable final NativeAdEventListener listener) {
        mNativeAdEventListener = listener;
    }

    @NonNull
    public String getPlacementId() {
        return mPlacementId;
    }

    public String getTitle() {
        return mBaseNativeAd.getTitle();
    }


    public String getDesc() {
        return mBaseNativeAd.getDesc();
    }

    public String getDesc2() {
        return mBaseNativeAd.getDesc2();
    }

    public String getSponsored() {
        return mBaseNativeAd.getSponsored();
    }

    public String getLikes() {
        return mBaseNativeAd.getLikes();
    }

    public String getDownloads() {
        return mBaseNativeAd.getDownloads();
    }

    public String getMainImageUrl() {
        return mBaseNativeAd.getMainImageUrl();
    }

    public String getIconImageUrl() {
        return mBaseNativeAd.getIconImageUrl();
    }

    public String getCallToAction() {
        return mBaseNativeAd.getCallToAction();
    }

    public Double getStarRating() {
        return mBaseNativeAd.getStarRating();
    }

    public void registerViewForAction(@NonNull final View view) {
        if (mIsDestroyed) {
            return;
        }
        mBaseNativeAd.registerViewForAction(view);
    }

    public void unregisterViewForAction(@NonNull View view) {
        if (mIsDestroyed) {
            return;
        }
        mBaseNativeAd.unregisterViewForAction(view);
    }

    public void destroy() {
        if (mIsDestroyed) {
            return;
        }
        mBaseNativeAd.destroy();
        mIsDestroyed = true;
    }


    public boolean isDestroyed() {
        return mIsDestroyed;
    }

    void notifyImpression() {
        if (mRecordedImpression || isDestroyed()) {
            return;
        }
        if (mNativeAdEventListener != null) {
            mNativeAdEventListener.onAdImpression();
        }
        mRecordedImpression = true;
    }

    void notifyClick() {
        if (isDestroyed()) {
            return;
        }
        if (mNativeAdEventListener != null) {
            mNativeAdEventListener.onAdClick();
        }
    }


    @Override
    public String toString() {
        return "[NativeAd, title:" + getTitle() + "@" + this.hashCode() + "]";
    }
}
